#
#   EventHandler.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: Salvatore S. Gionfriddo
#   Created: 2007.06.29
#   Last Modified: 2007.07.09
#

import pygame
from EvilGreg import EvilGreg


ENTER_KEY        = pygame.K_RETURN
QUIT_KEY         = pygame.K_ESCAPE
ADD_KID_KEY      = pygame.K_k
FPS_TOGGLE_KEY   = pygame.K_f
PAUSE_KEY        = pygame.K_p
MUSIC_TOGGLE_KEY = pygame.K_m

UP_KEY    = pygame.K_UP
DOWN_KEY  = pygame.K_DOWN
LEFT_KEY  = pygame.K_LEFT
RIGHT_KEY = pygame.K_RIGHT

PUNCH_KEY = pygame.K_s
KICK_KEY  = pygame.K_d
EAT_KEY   = pygame.K_SPACE
FLAME_KEY = pygame.K_a

        
def waitForKeyOrQuit(key):
    event = pygame.event.wait()
    
    while True:
        if event.type == pygame.KEYDOWN and event.key == key:
            break
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            quit(0)
        elif event.type == pygame.QUIT:
            quit(0)
        else:
            event = pygame.event.wait() 

class EventHandler(object):

    def __init__(self):
        None

    def doEvents(self, events):
        None

            
class GameEventHandler(EventHandler):
    
    def __init__(self, game):
        EventHandler.__init__(self)
        self.game = game
        self.gObjMan = self.game.gameObjectManager
        self.evilGreg = self.gObjMan.evilGreg
        
    def doEvents(self, events):
        for event in events:
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:

                if event.key == QUIT_KEY:
                    quit()
                elif event.key == ADD_KID_KEY:
                    self.gObjMan.addKid()
                elif event.key == FPS_TOGGLE_KEY:
                    self.game.hud.toggleFPS()
                    
                elif event.key == PAUSE_KEY:
                    self.game.pause = not self.game.pause #HACK
                elif event.key == MUSIC_TOGGLE_KEY:
                    self.game.toggleMusic()

                elif event.key == UP_KEY:
                    self.evilGreg.walk(EvilGreg.WALK_UP)
                elif event.key == DOWN_KEY:
                    self.evilGreg.walk(EvilGreg.WALK_DOWN)
                elif event.key == LEFT_KEY:
                    self.evilGreg.walk(EvilGreg.WALK_SIDE, EvilGreg.LEFT)
                elif event.key == RIGHT_KEY:
                    self.evilGreg.walk(EvilGreg.WALK_SIDE, EvilGreg.RIGHT)

                elif event.key == PUNCH_KEY:
                    self.evilGreg.punch()
                    
                elif event.key == KICK_KEY:
                    self.evilGreg.kick()
                    
                elif event.key == EAT_KEY:
                    self.evilGreg.eat()
                elif event.key == FLAME_KEY:
                    self.evilGreg.flame()
                    
            elif event.type == pygame.KEYUP:
                if event.key == UP_KEY:
                    self.evilGreg.stopWalk(EvilGreg.WALK_UP)
                elif event.key == DOWN_KEY:
                    self.evilGreg.stopWalk(EvilGreg.WALK_DOWN)
                elif event.key == LEFT_KEY:
                    self.evilGreg.stopWalk(EvilGreg.WALK_SIDE, EvilGreg.LEFT)
                elif event.key == RIGHT_KEY:
                    self.evilGreg.stopWalk(EvilGreg.WALK_SIDE, EvilGreg.RIGHT)

